package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.param.site.SiteParam;
import cn.ulc.socket.admin.model.param.site.SiteRegionParam;
import cn.ulc.socket.admin.service.site.SiteRegionServ;
import cn.ulc.socket.admin.service.site.SiteServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;


/*
*
* gen controller 2019-07-04
*/
@Validated
@RestController
@RequestMapping("/site")
public class SiteCtrl {

    @Autowired
    SiteServ siteServ;
    @Autowired
    SiteRegionServ siteRegionServ;

    private static final Log log = LogFactory.get();

    @GetMapping("/")
    public Result findListPage( String key,String cityCode,PaginationParam param) {
        return Result.success(siteServ.findList(key,cityCode,param));
    }

    @GetMapping(value = "/{id}")
    public Result findDetail (@PathVariable @Min(1) Long id) {
        return Result.success(siteServ.findDetail(id));
    }

    @PostMapping(value = "")
    public Result save(@Validated @RequestBody SiteParam param, BindingResult result) {
        if(result.hasErrors()) {
            log.error("保存分站详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(siteServ.save(param));
    }

    @PutMapping(value = "/{id}")
    public Result update(@PathVariable  Long id,@Validated @RequestBody SiteParam param, BindingResult result) {
        if(result.hasErrors()) {
            log.error("保存分站详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(siteServ.update(id,param));
    }

    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable @Min(1) Long id) {
        return Result.success(siteServ.delete(id));
    }

    @GetMapping("/findCityList")
    public Result findCityList() {
        return Result.success(siteServ.findCityList());
    }


    @PutMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Long id , AvailableStatus status){
        return Result.success(siteServ.changeStatus(id,status));
    }
    @PutMapping("/changeDefault/{id}")
    public Result changeDefault(@PathVariable("id") Long id){
        return Result.success(siteServ.changeDefault(id));
    }

    @GetMapping("/region/")
    public Result findRegionListPage( String key,Long siteId,PaginationParam param) {
        return Result.success(siteRegionServ.findListPage(key,siteId,param));
    }
    @GetMapping(value = "/region/{id}")
    public Result findRegionDetail (@PathVariable @Min(1) Long id) {
        return Result.success(siteRegionServ.findDetail(id));
    }
    @PostMapping(value = "/region/")
    public Result save(@Validated @RequestBody SiteRegionParam param, BindingResult result) {
        if(result.hasErrors()) {
            log.error("保存分站区域详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(siteRegionServ.save(param));
    }
    @PutMapping(value = "/region/{id}")
    public Result update(@PathVariable  Long id,@Validated @RequestBody SiteRegionParam param, BindingResult result) {
        if(result.hasErrors()) {
            log.error("保存分站区域详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(siteRegionServ.update(id,param));
    }

    @DeleteMapping(value = "/region/{id}")
    public Result deleteRegion(@PathVariable @Min(1) Long id) {
        return Result.success(siteRegionServ.delete(id));
    }

    @PutMapping("/region/changeStatus/{id}")
    public Result changeStatusRegion(@PathVariable("id") Long id , AvailableStatus status){
        return Result.success(siteRegionServ.changeStatus(id,status));
    }
}
